package com.huaguoguo.cyg.service.tl.buss;

import com.huaguoguo.cyg.converter.TLDTOConverter;
import com.huaguoguo.cyg.entity.tl.dto.TlServerDTO;
import com.huaguoguo.cyg.entity.tl.to.ServerInfo;
import com.huaguoguo.cyg.service.tl.data.ITlServerService;
import com.huaguoguo.cyg.spider.parse.TLCommonParser;
import com.huaguoguo.cyg.spider.processor.tl.SimpleHttpProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明： 天龙公共的服务<br/>
 * author:huaguoguo
 * Date: 2020/2/22
 * Time: 12:59
 */
@Service
public class TLCommonService {

    @Autowired
    private ITlServerService tlServerService;
    /**
     * 获取服务器列表
     * @return
     */
    public List<ServerInfo> getServerListByHttp(){
        String srvlist = SimpleHttpProcessor.getSrvlist();
        return TLCommonParser.parseServerList(srvlist);
    }

    /**
     * 更新数据库中的服务器列表
     * @return
     */
    public boolean saveOrUpdateServerInfo() {
        List<ServerInfo> serverInfos = getServerListByHttp();
        List<TlServerDTO> tlServerDTOS =
                serverInfos.stream().map(TLDTOConverter.tlServerInfoDTOconvert()).collect(Collectors.toList());
        return tlServerService.saveBatch(tlServerDTOS);
    }





}
